(function($){
    $.fn.thumbNav=function(settings){
      var defaults={
        maxWidth:               768,
        navBtnWidth:            18,
        borderWidth:            1,
        margin:                 3,
        navContainer:           '#thumbNav',
        thumbContainer:         '.thumbDiv',
        navBtn:                 '#navBtn'
      };
      
      $.extend(this,{
        buildPage: function(){
            var i=this.startThumb-1;
            var totalWidth=0;
            this.thumbArray.hide();
            do{totalWidth+=this.widthArray[i]+2*this.borderWidth+2*this.margin;}
            while(totalWidth<=this.maxWidth && ++i<this.thumbNum);
            if(i==this.startThumb-1) ++i;//第一张图片及超宽
            this.thumbArray.slice(this.startThumb-1,i).show();
            this.startThumb=i+1;
            if(this.startThumb>this.thumbNum){this.startThumb=1;}
            this.fadeTo(300,1.0);
            return this;
        },
        showNextPage: function(){
            this.fadeTo(300,0.0,function(){thumbNav.buildPage(this.startThumb);});
            return this;
        },
        init: function(){
            var totalWidth=0;
            var thumbWidth=new Array();
            $.each(this.thumbArray, function(key,val){
                totalWidth+=val.clientWidth+2*thumbNav.borderWidth+2*thumbNav.margin;
                thumbWidth.push(val.clientWidth);
            });
            this.widthArray=thumbWidth;
            if(totalWidth<=this.maxWidth+this.navBtnWidth) {this.navBtnDom.hide();this.maxWidth+=this.navBtnWidth;}
            else {this.navBtnDom.show();this.navBtnDom.click(function(){thumbNav.showNextPage();});}
            this.hide();
            this.buildPage();
            return this;
        }
      });
      $.extend(this, defaults, settings);
      var thumbNav=this;
      this.thumbArray=this.find('div'+this.thumbContainer);
      this.thumbNum=this.thumbArray.length;
      this.startThumb=1;
      this.widthArray=null;
      this.navBtnDom=this.find('div'+this.navBtn);
      
      this.init();
      return this;  
    };
})(jQuery);